﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataMappers;

using System.Data.SqlClient;
using ConcreteMappersAndObjects.DomainObjects;
using System.Data;

namespace ConcreteMappersAndObjects.Mappers
{
    public class StudentsDataMapper : AbstractDataMapper<int, Student> // primeiro criar objectos
    {
        private IEntityMapper<SubscriptionKey, Subscription> _subscriptionsMapper;


        public void SetSubscriptionsMapper(IEntityMapper<SubscriptionKey, Subscription> mapper)
        {
            _subscriptionsMapper = mapper;
        }
        protected override int GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "select ID, Number, Name from dbo.Students";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where ID = @ID";
        }

        protected override void BindFindStatement(SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@ID", key);
        }

        private class StudentsFilter : IFilter
        {
            private int _studendKey;
            public StudentsFilter(int studendKey)
            {
                _studendKey = studendKey;
            }
            #region IFilter Members

            public string GetWhereClause()
            {
                return " where StudentKey = @StudentID";
            }

            public void BindValues(SqlCommand cmd)
            {
                cmd.Parameters.AddWithValue("@StudentKey", _studendKey);
            }

            #endregion
        }
        protected override Student DoLoad(SqlDataReader sdr)
        {
            LazySet<SubscriptionKey, Subscription> subscriptions =
                new LazySet<SubscriptionKey,Subscription>(_subscriptionsMapper, new StudentsFilter(sdr.GetInt32(0)));
            return new Student(sdr.GetInt32(0), sdr.GetString(2), sdr.GetInt32(1), subscriptions);
            //throw new NotImplementedException();
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(SqlCommand cmd, Student t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "StudentInsert";
        }

        protected override void DoBindInsert(SqlCommand cmd, Student t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Number", SqlDbType.Int).Value = t.Number;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = t.Name;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from Students where ID = @ID";
        }

        protected override void DoBindRemove(SqlCommand cmd, Student t)
        {
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = t.ID;
        }

        protected override void FinishUpdateObject(Student t)
        {
            
        }

        protected override void FinishNewObject(Student t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(Student t)
        {
            
        }
    }
}
